//
//  SinaPictureView.swift
//  WeiBo01
//
//  Created by mac on 16/1/7.
//  Copyright © 2016年 Han. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let picMargin: CGFloat = 5
let itemMargin: CGFloat = 5
let itemWidth = (kUIScreenWidth - 2 * itemMargin - 2 * SinaHomeMargin) / 3

//可重用表示符
let SinaPictureViewReuseIdentifier = "SinaPictureViewReuseIdentifier"

class SinaPictureView: UICollectionView,UICollectionViewDataSource {
    
    //定义一个变量
    var pictures: [SinaPictureModel]?{
        
        didSet{
            //第一大步
            let size = getSize()
            
            snp_updateConstraints { (make) -> Void in
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            
//            textLabel.text = "\(pictures?.count ?? 0)"
            
            //刷新
            reloadData()
        }
    }

    
    init(){
        
        super.init(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
//        addSubview(textLabel)
//        
//        textLabel.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(self.snp_centerX)
//            make.centerY.equalTo(self.snp_centerY)
//        }
    
        //第二大步,设置数据源方法,设置布局
        //实现数据源方法
        dataSource = self
        //注册cell
        // !!!!!!!!!!!!!!!!!自定义的cell 一定要替换
        registerClass(SinaPictureViewCollectionViewCell.self, forCellWithReuseIdentifier: SinaPictureViewReuseIdentifier)
        
        // 设置 item
        // 获取系统的 布局,强制转换成流水布局
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        //间距
        flowLayout.minimumInteritemSpacing = itemMargin
        flowLayout.minimumLineSpacing = itemMargin

        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func getSize() -> CGSize {
        
        /*
        计算配图的宽高
        高度是图片的高度
       
        一张图片给他一个固定的宽高
        4张图片的时候返回一个 2*2的吧
        2,3 一个图片的高度,宽度是一个屏幕的宽度
        5,6, 2*图片的高度+间距,宽度 一个屏幕的宽度
        7,8,9 3*图片的高度+ 2*间距,宽度 一个屏幕的宽度
        */
        
        let count = pictures?.count
        
        if count == 1{
            
            return CGSizeMake(itemWidth, itemWidth)
        
        }else if count == 4{
            
            return CGSizeMake(itemWidth * 2 + itemMargin, itemWidth * 2 + itemMargin)
        
        }else {
            
            let row = (count! - 1) / 3 + 1
            let height  = CGFloat(row) * (itemWidth) + (CGFloat(row) - 1) * itemMargin
            
            return CGSizeMake(kUIScreenWidth - 2 * SinaHomeMargin, height)
        }
        
        return CGSizeZero
        
    }
    
    
 /// 测试label
    lazy var textLabel: UILabel =  UILabel()

}

extension SinaPictureView {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictures?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SinaPictureViewReuseIdentifier, forIndexPath: indexPath) as!SinaPictureViewCollectionViewCell
        
//        cell.backgroundColor = indexPath.row % 2 == 0 ? UIColor.yellowColor() : UIColor.redColor()
        
        cell.photoModel = pictures![indexPath.item]
        
        return cell
    }
    
}

//第三大步:自定义cell

class SinaPictureViewCollectionViewCell: UICollectionViewCell {
    
    //5设置模型
    var photoModel: SinaPictureModel?{
        
        didSet{
            //图片赋值
            let url = NSURL(string: photoModel?.thumbnail_pic ?? "")
            iconImageView.sd_setImageWithURL(url)
        }
    }
    
    
    //1
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //2.
    private func setupUI(){
        
        //4.
        addSubview(iconImageView)
        
        iconImageView.frame = bounds
        
    }
    
    //3.懒加载
    lazy var iconImageView: UIImageView = UIImageView()
    
}
